import 'dart:math';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vm;

/// 楼层类
class Floor {
  final int floorNumber;
  final List<Room3D> rooms;
  
  Floor({
    required this.floorNumber,
    required this.rooms,
  });
}

/// 3D房间类
class Room3D {
  final String roomNumber; // 如 "301", "302"
  final int floorNumber;
  final int roomIndex; // 该楼层中的房间索引
  final vm.Vector3 position; // 3D位置（房间左下角）
  final double width;
  final double depth;
  final double height;
  final bool isPlayerRoom; // 是否是玩家房间
  NPC3D? npc; // 房间的NPC（玩家房间为null）
  
  Room3D({
    required this.roomNumber,
    required this.floorNumber,
    required this.roomIndex,
    required this.position,
    required this.width,
    required this.depth,
    required this.height,
    this.isPlayerRoom = false,
    this.npc,
  });
  
  // 获取房间中心点
  vm.Vector3 get center => vm.Vector3(
    position.x + width / 2,
    position.y + height / 2,
    position.z + depth / 2,
  );
}

/// 3D NPC类
class NPC3D {
  final String name;
  final String roomNumber;
  final Color color; // NPC的颜色标识
  vm.Vector3 position; // 3D位置
  double width;
  double height;
  double depth;
  
  // 活动轨迹相关
  final List<ActivityPoint3D> activityPoints; // 活动点列表
  int currentActivityIndex = 0; // 当前活动点索引
  double moveSpeed = 1.0; // 移动速度
  bool isMoving = false;
  double waitTime = 0; // 等待时间（到达某个点后停留的时间）
  double currentWaitTime = 0; // 当前等待时间计数
  
  NPC3D({
    required this.name,
    required this.roomNumber,
    required this.color,
    required this.position,
    this.width = 15,
    this.height = 25,
    this.depth = 15,
    required this.activityPoints,
    this.moveSpeed = 1.0,
  });
  
  /// 更新NPC位置（根据活动轨迹）
  void update(double dt) {
    if (activityPoints.isEmpty) return;
    
    // 如果正在等待，减少等待时间
    if (currentWaitTime > 0) {
      currentWaitTime -= dt;
      if (currentWaitTime <= 0) {
        currentWaitTime = 0;
        isMoving = true;
        // 移动到下一个活动点
        currentActivityIndex = (currentActivityIndex + 1) % activityPoints.length;
      }
      return;
    }
    
    // 移动到当前目标点
    ActivityPoint3D target = activityPoints[currentActivityIndex];
    vm.Vector3 direction = target.position - position;
    double distance = (target.position - position).length;
    
    if (distance < 2) {
      // 到达目标点，开始等待
      position = target.position.clone();
      isMoving = false;
      currentWaitTime = target.waitTime;
    } else {
      // 继续移动
      isMoving = true;
      vm.Vector3 normalized = direction.normalized();
      vm.Vector3 movement = normalized * (moveSpeed * dt * 60); // 转换为每帧移动距离
      position = position + movement;
    }
  }
}

/// 3D NPC活动点
class ActivityPoint3D {
  final vm.Vector3 position;
  final double waitTime; // 在此点停留的时间（秒）
  final String? description; // 活动描述（可选）
  
  ActivityPoint3D({
    required this.position,
    this.waitTime = 2.0,
    this.description,
  });
}

/// 3D玩家类
class Player3D {
  vm.Vector3 position; // 3D位置
  double width;
  double height;
  double depth;
  vm.Vector3 velocity;
  bool isFacingRight;
  int currentFloor; // 当前所在楼层
  
  Player3D({
    required this.position,
    this.width = 20,
    this.height = 30,
    this.depth = 20,
    vm.Vector3? velocity,
    this.isFacingRight = true,
    this.currentFloor = 3,
  }) : velocity = velocity ?? vm.Vector3.zero();
}

